#include "cfghandle.h"

#include <QSettings>
#include <QFile>

CfgHandle::CfgHandle() {

}

CfgHandle::~CfgHandle() {}

void CfgHandle::init(QString cfg_url) {
    cfg_set = new QSettings(cfg_url, QSettings::IniFormat);
    cfg_set -> setIniCodec("UTF-8");

    if(!QFile(cfg_url).exists()) {
//        generate();
    }
}

QMap<QString, QVariant> CfgHandle::load(QString grp_name) {
    QMap<QString, QVariant> data;

    QStringList groups = cfg_set->childGroups();    // 获取所有组名

    if(!groups.contains(grp_name)) {
        return data;
    }

    cfg_set->beginGroup(grp_name);                  // 进入组
    QStringList keys = cfg_set->childKeys();        // 获取组内所有键名
    for (const QString &key : keys) {
        QVariant value = cfg_set->value(key);       // 获取键对应的值
        data.insert(key, value);
    }
    cfg_set->endGroup();                            // 退出组

    return data;
}

void CfgHandle::update(QString key, QString value) {
    cfg_set->setValue(key, value);
}

void CfgHandle::generate() {
    cfg_set->setValue("file/default_name", "test.json");
}
